import React from 'react'
// ===================================================================== Temp
import TableModal from '#cpt/temp/TableModal'
// ===================================================================== global declare
const { $fn, $Lazy, $http } = window
// ===================================================================== Lazy
const message = $Lazy.file(()=>import('@antd/message'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		forms : [
			{ label: '关键字',		name: 'perm_name' },
			{ label: '权限代码',		name: 'perm_code' },
			{ label: '权限类型',		name: 'perm_type',	type: 'select', idStr:'code' },
		]
	}
	
	model = {}
	
	open = async ()=> {
		let { route, uuid } = this.props
		
		let param = {}
		switch(route){
			case 'employee':
				param = { empl_id: uuid }
				break;
			case 'position':
				param = { pos_id: uuid }
				break;
			case 'role':
				param = { role_id: uuid  }
				break;
			case 'groups':
				param = { group_id: uuid  }
				break;
			case 'fl-process-auth':
				param = { process_id: uuid  }
				route = 'process'
				break;
			default:
				break;
		}
		this.query = { filter: route, ...param }
		this.modal.open()
		
		
		this.setState(({ forms }) => {
			forms[2].data = this.props.typeData
			return { forms }
		})
		
		this.fetch()
	}
	// paging
	fetch = async param => {
		const { data } = this.props
		const _data = await $fn.fetch.call(this, 'permission/index', param)
		
		if($fn.hasArray(data)){
			let selectedKeys = []
			_data.forEach(v=>{
				data.forEach(x=>{
					if(v.perm_code === x.perm_code){
						selectedKeys.push(v.uuid)
					}
				})
			})
			this.setState({ selectedKeys })
		}
	}
	
	// 表单
	cols = [
		{ title: '权限代码', 	dataIndex: 'perm_code', 			width:300 },
		{ title: '权限名称', 	dataIndex: 'perm_name', },
		{ title: '权限级别', 	dataIndex: 'level', 			width:80,  align:'center' },
		{ title: '权限类型', 	dataIndex: 'type', 	width:150 },
	]
	
	render(){
		const { onRefresh, route, uuid } = this.props
		
		return TableModal.call(this,{ title: '添加', labelWidth:'s7', width:'99%', onOk: selectedKeys =>{
			let api
			let param = {}
			switch(route){
				case 'employee':
					api = 'employee/addpermission'
					param = { empl_id: uuid }
					break;
				case 'position':
					api = 'position/addPermission'
					param = { pos_id: uuid }
					break;
				case 'role':
					api = 'role/addPermission'
					param = { role_id: uuid  }
					break;
				case 'groups':
					api = 'groups/addPermission'
					param = { group_id: uuid  }
					break;
				case 'fl-process-auth':
					api = 'fl-process-auth/addpermission'
					param = { process_id: uuid }
					break;
				default:
					break;
			}
			if(!api) return message.then(f=>f.error('接口不存在'))
			// 添加
			$http.submit(null, api, { param:{perm_id: selectedKeys, ...param } }).then(data=>{
				message.then(f=>f.success('添加成功'))
				this.modal.close()
				$fn.clearKeys.call(this)
				onRefresh?.()
			})
		}})
	}
}

export default Index